#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

//钩子函数
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
	if(code < 0)
	{
		//如果code小于0，必须调用CallNextHookEx传递消息，不处理该消息，并返回CallNextHookEx的返回值。
		return CallNextHookEx(g_Hook, code, wParam, lParam);
	}

	if(code == HC_ACTION && lParam > 0)
	{
		//code等于HC_ACTION，表示消息中包含按键消息
		//如果为WM_KEYDOWN，则显示按键对应的文本
		char szBuf[MAXBYTE] = {0};
		GetKeyNameText(lParam, szBuf, MAXBYTE);
		MessageBox(NULL, szBuf, u8"提示", MB_OK);
	}

	return CallNextHookEx(g_Hook, code, wParam, lParam);
}
void DLL_EXPORT SetHookOn()
{
    //安装钩子
	g_Hook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_Inst, 0);
}
void DLL_EXPORT SetHookOff()
{
    //卸载钩子
	UnhookWindowsHookEx(g_Hook);
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
//    switch (fdwReason)
//    {
//        case DLL_PROCESS_ATTACH:
//            // attach to process
//            // return FALSE to fail DLL load
//            break;
//
//        case DLL_PROCESS_DETACH:
//            // detach from process
//            break;
//
//        case DLL_THREAD_ATTACH:
//            // attach to thread
//            break;
//
//        case DLL_THREAD_DETACH:
//            // detach from thread
//            break;
//    }
    g_Inst = (HINSTANCE)hinstDLL;
    return TRUE; // succesful
}
